﻿using Microsoft.Xna.Framework;

namespace BattleFury.Lib.Graphics
{
	public class Camera
	{
		private float _zoom = 1;

		public Vector2 Position { get; set; }
		public Vector2 Size { get; set; }
		public Vector2 UpperBounds { get; set; }
		public float Rotation { get; set; }
		public bool ZeroBound { get; set; }
		public bool UpperBound { get; set; }

		public float Zoom
		{
			get { return _zoom; }
			set
			{
				_zoom = value;
				if (_zoom < 0.1f) _zoom = 0.1f;
			}
		}

		public int Width
		{
			get { return (int)(Size.X * Zoom); }
		}

		public int Height
		{
			get { return (int)(Size.Y * Zoom); }
		}

		public Rectangle Bounds
		{
			get { return new Rectangle((int)Position.X, (int)Position.Y, Width, Height); }
		}

		public Matrix Matrix
		{
			get
			{
				return Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0)) *
					   Matrix.CreateRotationZ(Rotation) *
					   Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
					   Matrix.CreateTranslation(new Vector3(0f, 0f, 0));
			}
		}

		public Camera()
		{
			ZeroBound = true;
			UpperBound = true;
		}

		public void Move(Vector2 direction)
		{
			Position += direction;

			if (ZeroBound)
			{
				if (Position.X <= 0)
					Position = new Vector2(0f, Position.Y);
				if (Position.Y <= 0)
					Position = new Vector2(Position.X, 0f);
			}

			if (UpperBound)
			{
				if (Position.X > UpperBounds.X)
					Position = new Vector2(UpperBounds.X, Position.Y);
				if (Position.Y > UpperBounds.Y)
					Position = new Vector2(Position.X, UpperBounds.Y);
			}
		}

		public void Reposition(Vector2 position)
		{
			Position = position;
		}
	}
}